package controllers;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import models.Orders;
import models.Resource;
import models.Trades;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import play.Logger;
import play.Play;
import play.data.validation.Required;

import com.etaoking.commons.BeanUtilsEx;
import com.etaoking.commons.Contants;
import com.etaoking.commons.TaobaoUtils;
import com.taobao.api.domain.Order;
import com.taobao.api.domain.Trade;


public class TradeAction extends AdminController{
	
	
	public static void index(){
		Logger.info("访问订单列表!");
		List<Trade> trades = TaobaoUtils.getTrades();
		List<String> orderStatus = new ArrayList<String>();
		for (Trade trade : trades) {
			List<Order> orders = trade.getOrders();
			for (Order order : orders) {
				if(!Orders.exist(order.getOid())){
					orderStatus.add(order.getOid().toString());
				}
			}
		}
		render(trades,orderStatus);
	}
	
	
	/**
	 * 查询所有的订单信息
	 *
	 * @param type 查询类询0未晒１已晒2全部
	 */
	public static void list(int type){
		Logger.info("访问订单列表!");
		List<String> orderStatus = new ArrayList<String>();
		if(type == 2){
			List<Trade> trades = TaobaoUtils.getTrades();
			for (Trade trade : trades) {
				List<Order> orders = trade.getOrders();
				for (Order order : orders) {
					if(Orders.exist(order.getOid())){
						orderStatus.add(order.getOid().toString());
					}
				}
			}
			render(trades,orderStatus);
		}else if(type == 1){
			List<Trades> trades = Trades.findAll();
			for (Trades trade : trades) {
				List<Orders> orders = trade.getOrders();
				for (Orders order : orders) {
					orderStatus.add(order.getOid().toString());
				}
			}
			render(trades,orderStatus);
		}else{
			List<Trade> trades = TaobaoUtils.getTrades();
			for (Trade trade : trades) {
				List<Order> orders = trade.getOrders();
				if(orders!=null && orders.size()>0){
					int length = orders.size();
					for (int i = 0; i < length; i++) {
						if(Orders.exist(orders.get(i).getOid())){
							orders.remove(i);
							length--;
						}
					}
				}
			}
			render(trades,orderStatus);
		}
		
	}
	
	/**
	 * 发布商品
	 * 
	 * @param order
	 * @return
	 */
	public static void addOrder(@Required(message="单据id不能为空!") Long tid,@Required String customTitle,@Required String recommendedReason,File photo){
		if(validation.hasErrors()) {
	        flash.error("Oops, please enter your name!");
	        index();
	    }
		if(Trades.exist(tid)){
			
		}
		Trade trade = TaobaoUtils.getTrade(tid);
		List<Order> orders = trade.getOrders();
		Orders order = new Orders();
		models.Trades myTrade = new models.Trades();
		try {
			BeanUtilsEx.copyProperties(myTrade, trade);
		} catch (InvocationTargetException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		}
		myTrade.save();
		for (Order order2 : orders) {
			try {
				BeanUtilsEx.copyProperties(order, order2);
				order.setTrade(myTrade);
				if(StringUtils.isNotBlank(customTitle))order.customTitle = customTitle;
				if(StringUtils.isNotBlank(recommendedReason))order.recommendedReason = recommendedReason;
				if(photo!=null){
					File file = new File(Play.configuration.getProperty("attachments.path")+"/"+UUID.randomUUID()+"."+StringUtils.substringAfterLast(photo.getName(), "."));
					try {
						Logger.info("保存上传文件至"+file.getPath()+"!");
						Resource pic = new Resource();
						pic.name = photo.getName();
						pic.describes = Contants.UserInfo.current().getNick()+"使用效果!";
						pic.code = file.getName();
						pic.path = file.getPath();
						List<Resource> coll = new ArrayList<Resource>();
						coll.add(pic);
						order.pictureEffects = coll;
						FileUtils.copyFile(photo, file);
					} catch (IOException e2) {
						e2.printStackTrace();
					}
				}
					
				order.save();
				
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		index();
	}
}
